﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace DarwinIsDead.Traits
{
    public class TimeLordTrait : Trait
    {
        private Texture2D sprite;
        float startTime;
        Player player;
        float currTime;
        int level;
        float teleportTime;

        public TimeLordTrait(Zombie owner, World world = null)
            : base(owner, world)
        {
            

        }


        protected override void AdjustLevel(int level)
        {
            teleportTime = 4f - .2f * level;

            if (teleportTime < .2)
            {
                teleportTime = .2F;
            }
        
        }

        public override void Initialize()
        {
            sprite = World.Game.Content.Load<Texture2D>("Sprites\\Traits\\tardis");
            teleportTime = 4;
            currTime = 0f;

            Owner.HitBox.Width += sprite.Bounds.Width;   // Add some dimension to the zombie's hitbox
            Owner.HitBox.Height += sprite.Bounds.Height;

            List<Entity> players = World.GetEntities<Player>();
            if (players.Count > 0)
            {
                player = (Player)players[0];
            }
        }

        protected override void InternalUpdate(GameTime gameTime)
        {
            float dt = gameTime.ElapsedGameTime.Milliseconds / 1000f;
            currTime += dt;

            if (player != null)
            {
                if (currTime > teleportTime)
                {

                    float dpx = player.Position.X - Owner.Position.X;
                    float dpy = player.Position.Y - Owner.Position.Y;
                    float direction = (float)Math.Atan2(dpy, dpx);
                    dpx *= 2;
                    dpy *= 2;

                    Owner.Move(dpx, dpy);

                    currTime = 0;


                }
            }
        }

        public override object Clone()
        {
            TimeLordTrait ret = new TimeLordTrait(Owner, World);
            return ret;
        }

        protected override void InternalDraw(SpriteBatch spriteBatch, Color color)
        {
            Vector2 drawPosition = new Vector2(Owner.Position.X, Owner.Position.Y);
            drawPosition.X += 30f;  // Draw the shoes a little lower on the body
            // And with some bounce in his stride

            // Draw the damn thing
            spriteBatch.Draw(sprite, drawPosition, color);
        }
    }


}
